<?php
namespace app\model;
class Activity extends \think\Model{
    public $table="activity";

    function getPage(){
        $res=$this->paginate();
        return $res;
    }

    function add_info($params){
        return $this->insert($params,false,true);
    }
    function edit_info($id,$params){
        unset($params['id']);
        return $this->where(['id'=>$id])->update($params);
    }
    function del_info($id){
        return $this->where(['id'=>$id])->delete();
    }
    function get_info($id){
        $arr=object2array($this->where(['id'=>$id])->find());
        if($arr!=false){
            $this->v_to_status($arr);
            return $arr;
        }else{
            return [];
        }
    }

    /**
     * 取得有的活动列表
     * @param array $params
     * @return mixed
     */
    function get_all_activity_list($params=[]){

        if(if_null($params['status'])){
            $params['status']=0;
        }
        $where=$this->status_to_where($params);
        //dump($where);
        $ob=$this->where($where)->select();
        //var_dump($ob->getLastSql());
        $arrs=object2array($ob);
        foreach($arrs as $k=>&$v){
            $this->v_to_status($v);


        }
        return $arrs;
    }

    function status_to_where($params){
        $cur_time=cur_time();
        if($params['status']==0){
            $where=[];
        }elseif($params['status']==1){
            /*
            $where=[
                ['hold_start_time','<=',$cur_time],
                ['hold_end_time','>=',$cur_time],
            ];

            */
            //$where=['hold_start_time','<=',$cur_time];
            $where['hold_start_time']=["<=",$cur_time];
            $where['hold_end_time']=[">=",$cur_time];
        }elseif($params['status']==2){
            /*
            $where=[

                ['hold_end_time','<=',$cur_time],
            ];
            */
            $where['hold_end_time']=['<=',$cur_time];

        }
        return $where;
    }

    function v_to_status(&$v){
        $cur_time=cur_time();
        if($v['hold_start_time']<=$cur_time && $v['hold_end_time']>=$cur_time){
            $status=1;
        }elseif($v['hold_end_time']<$cur_time){
            $status=2;
        }
        $v['status']=$status;

        $v['status_name']=config("web.activity_status")[$status];

    }

}